package cn.admin.configcenter.registry.service.client;

import cn.admin.config.common.protocol.ProtocolPackage;
import cn.admin.configcenter.registry.service.RecordCounterHolder;
import cn.admin.remoting.Connection;
import cn.admin.remoting.RequestProcessor;
import cn.admin.remoting.util.DIYExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executor;

public class ConfigPackageProcessor implements RequestProcessor<ProtocolPackage> {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigPackageProcessor.class);

    @Override
    public Class<ProtocolPackage> interest() {
        return ProtocolPackage.class;
    }

    @Override
    public void handleRequest(ProtocolPackage request, AppResponseOutput output) {
        Connection conn = output.getConnection();
        String clientIp = conn.getRemoteAddress();
        RecordCounterHolder.getHolder().increaseProcessRequestCount();
        ClientConnectionWorkers.dispatch(clientIp,new ClientRequestJob(request,output));
        LOG.debug("[submit] Connection=" + conn + ", " + request);
    }

    @Override
    public Executor getExecutor() {
        return DIYExecutor.getInstance();
    }

    @Override
    public void onRejectedExecutionException(ProtocolPackage appRequest,
                                             AppResponseOutput respOut) {
        throw new UnsupportedOperationException("never happen");
    }
}
